<!--专家已发布的知识-->
<template>
    <div class="published-message">
        <div style="padding:10px 20px 0">
            <el-button type="success" @click="publishKnowledgeClick">发布知识</el-button>
        </div>
        <div class="publish-content">
            <div class="message" v-for="(item, index) in userKnowledges" :key="index"
                :style="(index + 1) % 2 === 0 ? 'margin-right:0' : 'margin-right:20px'">
                <img v-if="item.picPath" class="knowleage-icon" :src="item.picPath" alt="" />
                <img class="knowleage-icon" v-else src="@/assets/default.jpg">
                <div class="info">
                    <h4 class="title">{{ item.title }}</h4>
                    <span class="initiator">发起人：{{ item.ownName }}</span>
                    <p class="content">{{ item.content }}</p>
                    <div class="btns-style">
                        <span @click="handelUpdateDialog(item)">
                            修改
                        </span>
                        <span @click="deleteInfo(item.knowledgeId)">
                            删除
                        </span>
                    </div>
                </div>
            </div>
        </div>


        <!-- 添加对话框 -->
        <el-dialog :close-on-click-modal="false" v-model="addDialogVisible" title="发布知识" width="500">

            <el-form :model="newKnowledge" label-width="80px">

                <el-form-item label="封面" prop="picPath">
                    <el-upload class="avatar-uploader" action="http://localhost:8090/file/upload" :headers=head
                        :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="newKnowledge.picPath" :src="newKnowledge.picPath" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>
                </el-form-item>

                <el-form-item label="标题">
                    <el-input v-model="newKnowledge.title" style="width:290px;"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input type="textarea" v-model="newKnowledge.content" style="width:290px;"></el-input>
                </el-form-item>
            </el-form>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="addDialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="add">发布</el-button>
                </div>
            </template>
        </el-dialog>


        <!-- 修改对话框 -->
        <el-dialog :close-on-click-modal="false" v-model="updateDialogVisible" title="修改知识" width="500">

            <el-form :model="newKnowledge" label-width="80px">

                <el-form-item label="封面" prop="picPath">
                    <el-upload class="avatar-uploader" action="http://localhost:8090/file/upload" :headers=head
                        :show-file-list="false" :on-success="handleAvatarSuccess" :before-upload="beforeAvatarUpload">
                        <img v-if="newKnowledge.picPath" :src="newKnowledge.picPath" class="avatar" />
                        <el-icon v-else class="avatar-uploader-icon">
                            <Plus />
                        </el-icon>
                    </el-upload>

                </el-form-item>
                <el-form-item label="标题">
                    <el-input v-model="newKnowledge.title" style="width:290px;"></el-input>
                </el-form-item>
                <el-form-item label="内容">
                    <el-input type="textarea" v-model="newKnowledge.content" style="width:290px;"></el-input>
                </el-form-item>
            </el-form>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="updateDialogVisible = false">取消</el-button>
                    <el-button type="primary" @click="update">发布</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>


<script setup lang="ts">

import { deleteKnowledgeById, updateKnowledgeById, addKnowledge, selectKnowledgeByUsername, } from "@/api/knowledge";
import { ref } from "vue";
import store from "@/store";
import { ElMessage } from "element-plus";
import type { UploadProps } from 'element-plus'
const picture = ref('');
const userKnowledges = ref([]);
const updateInfo = ref({});
const newKnowledge = ref({
    picPath: picture.value,
    title: "",
    content: "",
});
const addDialogVisible = ref(false);
const updateDialogVisible = ref(false);

const load = () => {
    picture.value = ''
    newKnowledge.value = {
        picPath: picture.value,
        title: "",
        content: "",
    };

    selectKnowledgeByUsername().then((res) => {
        userKnowledges.value = res.data;
    })
}
load()

//打开发布知识对话框
const publishKnowledgeClick = () => {
    addDialogVisible.value = true;
}

//发布知识
const add = () => {
    addKnowledge(newKnowledge.value).then((res) => {
        if (res.flag) {
            load();
            addDialogVisible.value = false;
            ElMessage.success("发布成功！");
        } else {
            ElMessage.error("发布失败！");
        }
    })
}


//打开修改知识的发布对话框
const handelUpdateDialog = (item) => {
    newKnowledge.value = item
    updateDialogVisible.value = true;
}


//update
const update = () => {
    updateKnowledgeById(newKnowledge.value).then((res) => {
        if (res.flag) {
            load();
            updateDialogVisible.value = false;
            ElMessage.success("修改成功！");
        } else {
            ElMessage.error("修改失败！");
        }
    })
}

//删除
const deleteInfo = (val) => {
    deleteKnowledgeById({ knowledgeId: val }).then(res => {
        if (res.flag) {
            ElMessage.success('删除成功');
            load();
        } else {
            ElMessage.error('删除失败');
        }
    })
}

//文件上传
const handleAvatarSuccess: UploadProps['onSuccess'] = (
    response,
    uploadFile
) => {
    console.log(response);
    newKnowledge.value.picPath = response.data;
    picture.value = response.data;
}

const beforeAvatarUpload: UploadProps['beforeUpload'] = (rawFile) => {
    if (rawFile.type !== 'image/jpeg') {
        ElMessage.error('Avatar picture must be JPG format!')
        return false
    } else if (rawFile.size / 1024 / 1024 > 2) {
        ElMessage.error('Avatar picture size can not exceed 2MB!')
        return false
    }
    return true
}


const head = ref({
    "Authorization": store.state.token
})

</script>

<style lang="scss" scoped>
.search {
    background-color: white;
    background-color: white;
    display: flex;
    height: 35px;
    line-height: 35px;

    .search-icon {
        position: relative;
        left: -2px;
        cursor: pointer;
    }
}

.btns-style {
    display: flex;
    width: 100%;
    justify-content: space-around;
    color: green;
    margin-top: 20px;

    span {
        cursor: pointer;
    }
}

.published-message {
    color: black;
    text-align: left;
    width: 900px;
    margin: 0 auto;
    height: 100%;
    background: #fff;

    .publish-content {
        display: flex;
        margin-top: 10px;
        flex-wrap: wrap;
    }

    .message {
        box-sizing: border-box;
        width: 410px;
        height: 180px;
        border: 1px solid #f2f2f2;
        background-color: white;
        border-radius: 6px;
        display: flex;
        flex-direction: row;
        justify-content: flex-start;
        margin: 10px 20px 10px 0;
        background: #fff;

        // padding: 10px 20px;
        img {
            width: 200px;
            height: 180px;
            margin-right: 10px;
            border-radius: 6px;
        }

        .info {
            margin-top: 10px;

            .item-style {
                height: 20px;
                line-height: 20px;
            }

            .initiator {
                color: #666;
            }

            .title {
                font-size: 16px;
                height: 30px;
                /*超出的部分隐藏*/
                overflow: hidden;
                /*文字用省略号替代超出的部分*/
                text-overflow: ellipsis;
                /*弹性伸缩盒子模型显示*/
                display: -webkit-box;
                /*限制在一个块元素显示文本的行数*/
                -webkit-line-clamp: 3;
                /*设置或检索伸缩盒对象的子元素排列方式*/
                -webkit-box-orient: vertical;
            }

            .content {
                width: 170px;
                line-height: 20px;
                max-height: 80px !important;
                margin-top: 10px;
                word-break: break-all;
                /*超出的部分隐藏*/
                overflow: hidden;
                /*文字用省略号替代超出的部分*/
                text-overflow: ellipsis;
                /*弹性伸缩盒子模型显示*/
                display: -webkit-box;
                /*限制在一个块元素显示文本的行数*/
                -webkit-line-clamp: 3;
                /*设置或检索伸缩盒对象的子元素排列方式*/
                -webkit-box-orient: vertical;
            }

            .price {
                font-size: 16px;
                font-weight: bold;
                display: block;
                color: red;
            }
        }
    }

    .published-message-operation {
        margin-bottom: 5px;
        display: flex;
        flex-direction: row;
        justify-content: flex-end;
        align-items: center;
    }
}

.search .el-input--suffix .el-input__inner {
    height: 35px;
    line-height: 35px;
}


.avatar-uploader .avatar {
    width: 178px;
    height: 178px;
    display: block;
}


.avatar-uploader .el-upload {
    border: 1px dashed var(--el-border-color);
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
    transition: var(--el-transition-duration-fast);
}

.avatar-uploader .el-upload:hover {
    border-color: var(--el-color-primary);
}

.el-icon.avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 178px;
    height: 178px;
    text-align: center;
}
</style>